package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Fengkai
 * @creat 2023/3/29
 */
public class SheJiQuestion {

       public static void main(String[] args){
           //15
           //3,3,7,4,4,4,4,7,7,3,5,5,5,6,6
           //53,80,68,24,39,76,66,16,100,55,53,80,55,10,10
           Scanner scanner = new Scanner(System.in);
           Integer count = Integer.valueOf(scanner.nextLine());
           String[] firingOrder = scanner.nextLine().split(",");
           List<String> collect = Arrays.stream(firingOrder).collect(Collectors.toList());
           String[] socres = scanner.nextLine().split(",");

           //每个人得分情况
           HashMap<Integer, List<Integer>> idScoreCache = new HashMap<>();
           //最终每个人得分
           Map<Integer, Integer> idScoreMap = new HashMap<Integer, Integer>();

           for (int i = 0; i <= count - 1; i++) {
               if (idScoreCache.containsKey(Integer.valueOf(firingOrder[i]))){
                   idScoreCache.get(Integer.valueOf(firingOrder[i])).add(Integer.valueOf(socres[i]));
               }else {
                   ArrayList<Integer> integers = new ArrayList<>();
                   integers.add(Integer.valueOf(socres[i]));
                   idScoreCache.put(Integer.valueOf(firingOrder[i]), integers);
               }
           }

           for (Integer integer : idScoreCache.keySet()) {
               if (idScoreCache.get(integer).size() >= 3){
                   //统计个人分数  要最高的三个分数之和
                   List<Integer> integers = idScoreCache.get(integer).stream().sorted((v1, v2) -> v2.compareTo(v1)).collect(Collectors.toList()).subList(0, 3);
                   idScoreMap.put(integer, integers.stream().reduce(Integer::sum).get());
               }
           }
           Integer[] out = idScoreMap.keySet().toArray(new Integer[idScoreMap.size()]);
           //冒泡排序
           for (int i = out.length - 1; i >= 0 ; i--) {
               for (int j = out.length - 1 ; j >= 1 ; j--) {
                   if (idScoreMap.get(out[j]) > idScoreMap.get(out[j - 1])){
                       Integer tmp = out[j - 1];
                       out[j - 1] = out[j];
                       out[j] = tmp;
                   }else if (Objects.equals(idScoreMap.get(out[j - 1]), idScoreMap.get(out[j]))){
                       if (out[j - 1] < out[j]){
                           Integer tmp = out[j - 1];
                           out[j - 1] = out[j];
                           out[j] = tmp;
                       }
                   }
               }
           }
           StringBuffer stringBuffer = new StringBuffer();
           Arrays.stream(out).forEach( v -> {
               stringBuffer.append(v + ",");
           });
           stringBuffer.substring(0, stringBuffer.length() - 1);
           System.out.println(stringBuffer);
          }

}
